package st.business.tepth.business.taskallocation.returnfactory;

import com.alibaba.fastjson.TypeReference;

import io.reactivex.disposables.Disposable;
import st.business.tepth.base.OnDataConvertListener;
import st.business.tepth.business.bean.ReturnFactoryBean;
import st.business.tepth.config.Url;
import st.shadow.library.core.net.callback.IError;
import st.shadow.library.core.net.callback.ISuccess;
import st.shadow.library.core.net.data.ObjConverter;
import st.shadow.library.core.net.rx.RxRestClient;

/**
 * Author: shadow
 * Email:  1025431817@qq.com
 * Time:   2019-09-02 18:13
 */

public class ReturnFactoryModel implements ReturnFactoryContract.Model {

    @Override
    public Disposable getData(String token, int repairId, final OnDataConvertListener<ReturnFactoryBean> listener) {
        return RxRestClient.builder()
                .url(Url.RepairTransferFactory)
                .params("token", token)
                .params("repairId", repairId)
                .success(new ISuccess() {
                    @Override
                    public void onSuccess(String response) {
                        listener.convert(convertToData(response));
                    }
                })
                .error(new IError() {
                    @Override
                    public void onError(int code, String msg) {
                        listener.error(code, msg);
                    }
                })
                .build()
                .post();
    }

    @Override
    public Disposable returnFactory(String token, int repairId, String phenomenonIds,
                                    String maintenanceIds, int rescueFlag,
                                    final OnDataConvertListener<String> listener) {
        return RxRestClient.builder()
                .url(Url.AdjustRepairTransferFactory)
                .params("token", token)
                .params("repairId", repairId)
                .params("phenomenonIds", phenomenonIds)
                .params("maintenanceIds", maintenanceIds)
                .params("rescueFlag",rescueFlag)
                .success(new ISuccess() {
                    @Override
                    public void onSuccess(String response) {
                        listener.convert(response);
                    }
                })
                .error(new IError() {
                    @Override
                    public void onError(int code, String msg) {
                        listener.error(code, msg);
                    }
                })
                .build()
                .post();
    }

    private ReturnFactoryBean convertToData(String response) {
        return new ObjConverter<ReturnFactoryBean>().convert(response
                , new TypeReference<ReturnFactoryBean>() {
                });
    }
}
